package desmoj.extensions.visualization2d.animation.internalTools;

import java.awt.Dimension;
import java.util.Vector;

import desmoj.extensions.visualization2d.engine.command.Command;
import desmoj.extensions.visualization2d.engine.command.Parameter;
import desmoj.extensions.visualization2d.engine.model.EntityType;

/**
 * Used for EntityType Definition in every static part of
 * SimProcess and Entity Class. also used in initEntityTypes() 
 * method of ModelAnimation to create "createEntityType" commands.
 * 
 * @version DESMO-J, Ver. 2.4.1 copyright (c) 2014
 * @author christian.mueller@th-wildau.de
 *         For information about subproject: desmoj.extensions.visualization2d
 *         please have a look at: 
 *         http://www.th-wildau.de/cmueller/Desmo-J/Visualization2d/ 
 * 
 *         Licensed under the Apache License, Version 2.0 (the "License"); you
 *         may not use this file except in compliance with the License. You may
 *         obtain a copy of the License at
 *         http://www.apache.org/licenses/LICENSE-2.0
 * 
 *         Unless required by applicable law or agreed to in writing, software
 *         distributed under the License is distributed on an "AS IS" BASIS,
 *         WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 *         implied. See the License for the specific language governing
 *         permissions and limitations under the License.
 *
 */
public class EntityTypeAnimation {
	private String			id;
	private String			genereratedBy;
	private Dimension 		iconSize;
	private Vector<String>	possibleStates;
	private Vector<String>	possibleAttributes;
	private int				show;

	public EntityTypeAnimation(){
		this.possibleStates 	= new Vector<String>();
		this.possibleAttributes = new Vector<String>();
		this.id					= null;
		this.genereratedBy		= null;
		this.iconSize			= new Dimension(30,30);
		this.show				= EntityType.SHOW_NAME | EntityType.SHOW_ICON;
	}
	
	/**
	 * Normally, the id is the simple class name of SimProcess or Entity
	 * @param id
	 */
	public void setId(String id){
		this.id	= id;
	}
	
	public String getId(){
		return id;
	}
	
	/**
	 * informs about object that generate this animation command
	 * @param genereratedBy
	 */
	public void setGenereratedBy(String genereratedBy){
		this.genereratedBy	= genereratedBy;
	}
	
	/**
	 * All icons that animates entities of this entityType
	 * must have the same size
	 * @param size
	 */
	public void setIconSize(Dimension size){
		iconSize		= size;
	}
	
	/**
	 * set a combination of show flags from EntityType.SHOW_
	 * @param show
	 */
	public void setShow(int show){
		this.show	= show;
	}
	
	/**
	 * Defines the possible states and with which icon this state will be animated.
	 * The iconId must be defined before in ModelAnimation constructor.
	 * @param state
	 * @param iconId
	 */
	public void addPossibleState(String state, String iconId){
		String[] arg = {state, iconId};
		possibleStates.add(Parameter.cat(arg));
	}
	
	/**
	 * Defines possible Attributes
	 * Unvalid signs ;,:| are changed to _
	 * @param attribute
	 */
	public void addPossibleAttribute(String attribute){
		// replace unvalid signs in cmd syntax
		attribute.replace(';', '_');
		attribute.replace(',', '_');
		attribute.replace(':', '_');
		attribute.replace('|', '_');
		possibleAttributes.add(attribute);
	}
	
	/**
	 * generate "createEntityType" command in cmds-file
	 * @param initTime
	 * @return
	 */
	public Command getEntityTypeCmd(long initTime){
		Command entityTypeCmd = null;
		checkEntityTypeData();
		entityTypeCmd = Command.getCommandInit("createEntityTyp", initTime);
		entityTypeCmd.addParameter("EntityTypId", this.id);
		entityTypeCmd.addParameter("IconWidth", String.valueOf(iconSize.width));
		entityTypeCmd.addParameter("IconHeigth", String.valueOf(iconSize.height));
		for(int i=0; i<possibleStates.size(); i++){
			entityTypeCmd.addParameter("PossibleState", possibleStates.get(i));
		}
		for(int i=0; i<possibleAttributes.size(); i++){
			entityTypeCmd.addParameter("PossibleAttribute", possibleAttributes.get(i));
		}
		entityTypeCmd.addParameter("Show", String.valueOf(this.show));
		entityTypeCmd.setRemark("generated by "+genereratedBy);
		return entityTypeCmd;
	}
	
	
	/**
	 * used by getEntityTypeCmd
	 */
	private void checkEntityTypeData(){
		if(this.id == null)
			throw new SimulationException("EntityTypeAnimation: entityTypeId isn't set.");
		if(this.genereratedBy == null)
			throw new SimulationException("EntityTypeAnimation: genereratedBy isn't set.");
		/*
		boolean active = false;
		for(int i=0; i<possibleStates.size(); i++){
			System.out.println(possibleStates.get(i));
			if(Parameter.split(possibleStates.get(i))[0].equals("active")){
				active = true;
				break;
			}
		}
		if(! active)
			throw new SimulationException("SimProcessAnimation: no \"active\" state exists.");
		*/
	}
	

}
